﻿using System;
using System.Linq.Expressions;
using AutoMapper;
using MovieDB.Common;
using MovieDB.DTOs;
using MovieDB.Entities;
using MovieDB.Mapping.CustomConverter;
using MovieDB.Models;

namespace MovieDB.Mapping
{
    public static class MappingInitializer
    {
        public static void InitMapping()
        {
            InitEntityDtoMapping();
            InitModelDtoMapping();
            InitAutoCompleteDtoMapping();
        }

        private static void InitAutoCompleteDtoMapping()
        {
            Mapper.CreateMap<MovieDto, AutocompleteItem>()
                .ForMember(dest => dest.value, opt => opt.MapFrom(source => source.Title));
        }

        private static void InitEntityDtoMapping()
        {
            Mapper.CreateMap<Movie, MovieDto>();
            Mapper.CreateMap<MovieDto, Movie>()
                .ForMember(dest => dest.AudioQualities, opt => opt.Ignore())
                .ForMember(dest => dest.VideoQuality, opt => opt.Ignore());

            Mapper.CreateMap<VideoQuality, VideoQualityDto>();
            Mapper.CreateMap<AudioQuality, AudioQualityDto>();

            Mapper.CreateMap<VideoQualityDto, VideoQuality>();
            Mapper.CreateMap<AudioQualityDto, AudioQuality>()
                .ForMember(dest => dest.Movies, opt => opt.Ignore());
        }

        private static void InitModelDtoMapping()
        {
            Mapper.CreateMap<MovieDto, MovieModel>()
                .ForMember(dest => dest.AudioQualitiesAsString, opt => opt.ResolveUsing<AudioQualityStringResolver>())
                .ForMember(dest => dest.VideoQualitiesAsString, opt => opt.ResolveUsing<VideoQualityStringResolver>())
                .ForMember(dest => dest.MovieGenreAsString, opt => opt.ResolveUsing<MovieGenreStringResolver>())
                .ForMember(dest => dest.Type, opt => opt.UseValue(ModelCRUDType.Update))
                .ForMember(dest => dest.CreatedAt, opt => opt.MapFrom(source => source.CreatedAt.ToShortDateString()));

            Mapper.CreateMap<MovieModel, MovieDto>()
                .ForMember(dest => dest.VideoQuality, mapper => mapper.MapFrom(src => src.VideoQuality))
                .ForMember(dest => dest.AudioQualities, mapper => mapper.MapFrom(src => src.AudioQualities))
                .ForMember(dest => dest.CreatedAt, opt => opt.ResolveUsing<MovieDtoCreatedAtResolver>());
                                                                          

            Mapper.CreateMap<VideoQualityDto, VideoQualityModel>();
            Mapper.CreateMap<AudioQualityDto, AudioQualityModel>();

            Mapper.CreateMap<VideoQualityModel, VideoQualityDto>()
                .ForMember(dest => dest.Id, opt => opt.Ignore());
            Mapper.CreateMap<AudioQualityModel, AudioQualityDto>()
                .ForMember(dest => dest.Id, opt => opt.Ignore());

            Mapper.CreateMap<CategoryDto, CategoryModel>();
            Mapper.CreateMap<CategoryModel, CategoryDto>();
        }
    }
}